package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqDetail;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqListInfo;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqListRequest;
import org.dtrd.modules.evaluation.entity.bean.parq.ParqPageRequest;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdParq;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdParqMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdParqService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-08-15
 */
@DS("multi-datasource1")
@Service
public class DtrdEntEvaluationRdParqServiceImpl extends ServiceImpl<DtrdEntEvaluationRdParqMapper, DtrdEntEvaluationRdParq> implements IDtrdEntEvaluationRdParqService {

    private DtrdEntEvaluationRdParqMapper dtrdEntEvaluationRdParqMapper;

    @Override
    public List<ParqListInfo> getParqList(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdParq> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntEvaluationRdParq::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdParq::getCreateTime);
        List<DtrdEntEvaluationRdParq> poList = list(queryWrapper);
        List<ParqListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(po -> new ParqListInfo().parseFromPo(po)).collect(Collectors.toList());
        }
        return dtoList;
    }

//    @Override
//    public IPage<ParqListInfo> pageParq(ParqPageRequest request) {
//        LambdaQueryWrapper<DtrdEntEvaluationRdParq> queryWrapper = baseQueryWrapper();
//        IPage<DtrdEntEvaluationRdParq> page = new Page<>(request.getPageNum(), request.getPageSize());
//        page = this.page(page, queryWrapper);
//        return page.convert(po -> new ParqListInfo().parseFromPo(po));
//    }

//    @Override
//    public ParqDetail getParq(Integer dataId) {
//        DtrdEntEvaluationRdParq dtrdEntEvaluationRdParq = getById(dataId);
//        return Optional.ofNullable(dtrdEntEvaluationRdParq)
//                        .map(po -> new ParqDetail().parseFromPo(po))
//                        .orElse(null);
//    }

    @Override
    public boolean saveOrUpdateParq(ParqDetail dto) {
        DtrdEntEvaluationRdParq dtrdEntEvaluationRdParq = new DtrdEntEvaluationRdParq();
        StringJoiner joiner = new StringJoiner(",");
        if(dto.getDataId() == null || dto.getDataId()==0){
            System.out.println("我不是等于true吗");
            dtrdEntEvaluationRdParq = dtrdEntEvaluationRdParq.init();
        }
        dtrdEntEvaluationRdParq.parseFromDto(dto);
        List<Integer> answers = dto.getAnswers();
        int score = 0;
        for(Integer i: answers){
            // 如果结果填写为是则integer等于1，填写否则为0
            score += i;
            joiner.add("" + i);
        }
        dtrdEntEvaluationRdParq.setScore(score);
        dtrdEntEvaluationRdParq.setInput(joiner.toString());
        if(score <= 0){
            dtrdEntEvaluationRdParq.setOutput(Evaluation.PARQResult.ALLNOT.getResult());
        }else {
            dtrdEntEvaluationRdParq.setOutput(Evaluation.PARQResult.HASYES.getResult());
        }
        return saveOrUpdate(dtrdEntEvaluationRdParq);
    }

    @Override
    public boolean removeParq(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public ParqListInfo getCurrentParq(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdParq> wrapper = baseQueryWrapper()
                .eq(DtrdEntEvaluationRdParq::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdParq::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdParq dtrdEntEvaluationRdParq = getOne(wrapper);
        ParqListInfo parqInfo = new ParqListInfo();
        if(dtrdEntEvaluationRdParq != null){
            parqInfo = parqInfo.parseFromPo(dtrdEntEvaluationRdParq);
        }
        return parqInfo;
    }

    private LambdaQueryWrapper<DtrdEntEvaluationRdParq> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntEvaluationRdParq.class)
                        .eq(DtrdEntEvaluationRdParq::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntEvaluationRdParq> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntEvaluationRdParq.class)
                        .eq(DtrdEntEvaluationRdParq::getIsDel, 0);
    }
}
